//
//  SRCurrent+Cate.m
//  SportRing
//
//  Created by mykoma on 14/11/12.
//  Copyright (c) 2014年 HotOne. All rights reserved.
//

#import "SRCurrent+Cate.h"
#import "SRDB.h"

@implementation SRCurrent (Cate)

+ (SRCurrent *)currentForMe:(SRMe *)me
{
    __block SRCurrent * current = nil;
    [MagicalRecord saveUsingCurrentThreadContextWithBlockAndWait:^(NSManagedObjectContext *localContext)
     {
         current = [SRCurrent MR_createEntity];
         current.me = me;
     }];
    return current;
}

- (void)configAttributesWithBlock:(void(^)(SRCurrent * aSelf))block
{
    [MagicalRecord saveUsingCurrentThreadContextWithBlockAndWait:^(NSManagedObjectContext *localContext)
     {
         block(self);
     }];
}

@end

@implementation SRCurrent (Find)

+ (SRCurrent *)findCurrentForMe:(SRMe *)me
{
    return me.current;
}

+ (SRCurrent *)findCurrentCINEForMe:(SRMe *)me
{
    SRCurrent * current = [SRCurrent findCurrentForMe:me];
    if (current == nil) {
        current = [SRCurrent currentForMe:me];
    }
    return current;
}

- (CGFloat)averageTemperature
{
    return self.data.tempreature.floatValue;
}

- (CGFloat)altitude
{
    CGFloat  pressue = self.data.pressure.floatValue;
    if (pressue == 0) {
        return 0;
    }
    else
    {
        return 44330.0 *(1.0 - pow(pressue / 101.325, 0.1903));
    }
}

@end
